<template>
  <div class="upload-box">
    <el-upload
      :on-remove="handleRemove"
      :on-preview="preview"
      :file-list="fileList"
      list-type="picture-card"
      :class="{ disabled: fileComputed }"
      :limit="1"
      action="#"
      :http-request="upload"
      :on-change="handleChange"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- <el-progress
      style="width:300px"
      text-inside:stroke-width="20"
      :percentage="Percent"
    > -->
    <el-dialog
      width="600px"
      top="8vh"
      title="图片预览"
      :visible.sync="showDialog"
    >

      <img width="100%" :src="imgUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
// SecretId: AKIDYw4xaw1rQijUyA5j7E5e29zXpqb5INBi

// SecretKey: HJ4YlhLioeEl10jBMGuHNCk23mEQL1Ra
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDYw4xaw1rQijUyA5j7E5e29zXpqb5INBi', // 身份识别ID
  SecretKey: 'HJ4YlhLioeEl10jBMGuHNCk23mEQL1Ra' // 身份秘钥
})
export default {
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      fileList: [],
      showDialog: false, // 控制显示弹层
      imgUrl: ''
    //    Percent: 0
    }
  },
  computed: {
    // 设定一个计算属性 判断是否已经上传完了一张
    fileComputed() {
      return this.fileList.length >= this.limit
    },
    isAllUp() {
      return this.fileList.every(item => item.status === 'success')
    }
  },
  methods: {
    preview(file) {
      // 这里应该弹出一个层 层里是点击的图片地址
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter((item) => item.uid !== file.uid)
      console.log(this.fileList)
      console.log(file)
    },
    upload(obj) {
      console.log('可以上传了', obj)
      const uid = obj.file.uid
      const fileObj = this.fileList.find(item => item.uid === uid)
      fileObj.status = 'uploading'
      cos.putObject(
        {
          Bucket: 'human-1308187557', // 存储筒名称 /* 必须 */
          Region: 'ap-shanghai' /* 存储桶所在地域，必须字段 */,
          Key: parseInt(Math.random() * 9999) + obj.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: obj.file,
          // 上传文件对象
          onProgress: progressData => {
            // this.percent = parseInt(progressData.percent * 100)
            fileObj.percentage = parseInt(progressData.percent * 100)
          }
        }, (err, data) => {
          console.log(err || data)
          fileObj.status = 'success'
          fileObj.url = 'http://' + data.Location
          setTimeout(() => {
            fileObj.status = 'success'
          }, 1000)
        }

      )
    },
    handleChange(file, fileList) {
      this.fileList = [...fileList]
    }, // 配置上传前的校验, 只要通过校验, 才能进行上传
    beforeUpload(file) {
      // 1. 限制文件类型 file.type

      // 2. 限制文件大小 file.size
      if (file.size / 1024 / 1024 >= 5) {
        this.$message.error('上传头像过大, 超过了5M, 必须5M以内')
        return false
      }
      return true
    }
  }
}
</script>
<style lang="scss" scoped>
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>
